package com.modlin.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DeleteTeacher extends com.modlin.selenium.pages.TestBase {
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();

	@Test
	public void testDeleteTeacher() throws Exception {
		driver.get(baseUrl + "/test/en/Teacher");
		driver.findElement(By.linkText("Add New")).click();
		for (int second = 0;; second++) {
			if (second >= 60)
				fail("timeout");
			try {
				if (isElementPresent(By.cssSelector("button.butn.flR")))
					break;
			} catch (Exception e) {
			}
			Thread.sleep(1000);
		}

		driver.findElement(By.id("UserModel_FirstName")).clear();
		driver.findElement(By.id("UserModel_FirstName")).sendKeys("vadim");
		driver.findElement(By.id("UserModel_LastName")).clear();
		driver.findElement(By.id("UserModel_LastName")).sendKeys("qa");
		driver.findElement(By.id("UserModel_Password")).clear();
		driver.findElement(By.id("UserModel_Password")).sendKeys("12345678");
		driver.findElement(By.id("UserModel_ConfirmPassword")).clear();
		driver.findElement(By.id("UserModel_ConfirmPassword")).sendKeys(
				"12345678");
		driver.findElement(By.id("UserModel_Email")).clear();
		driver.findElement(By.id("UserModel_Email")).sendKeys(
				"vadimqa10@gmail.com");
		driver.findElement(By.cssSelector("button.butn.flR")).click();
		for (int second = 0;; second++) {
			if (second >= 60)
				fail("timeout");
			try {
				if (isElementPresent(By.linkText("vadim qa")))
					break;
			} catch (Exception e) {
			}
			Thread.sleep(1000);
		}

	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}
